<script lang="ts" setup>

import axios from 'axios';
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/store/user';

const userStore = useUserStore()
let form = ref({
    username:"",
    password:"",

})
let router = useRouter();
function onSubmit(){
    console.log(form.value)

    axios.post("http://localhost:8080/api/auth/login",form.value).then(res=>{
        console.log(res.data)
        localStorage.setItem("token",res.data.data.token)
        localStorage.setItem("username",res.data.data.username)
        localStorage.setItem("userid",res.data.data.userid)
        //跳转到首页
        router.replace("/")
    }).then(async ()=>{
        const res = await userStore.fetchMenus()
        
    })
    .catch(err=>{
        console.log(err.response.data.message)
        alert(err.response.data.message)
    })

  
}

</script>

<template>
    <h1>系统登录</h1>
    <el-form :model="form" label-width="auto" style="max-width: 600px">
      <el-form-item label="用户名">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" />
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" @click="onSubmit">登录</el-button>
        
      </el-form-item>
    </el-form>
  </template>

<style scoped>

</style>